<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\game\Groups;
use app\common\model\game\Lists;
use app\common\model\game\Record;
use app\common\model\task\Userlog;
use app\common\model\user\Bindgame;
use think\Db;
use think\Exception;

class Game extends Api
{
    protected $noNeedLogin = ["lists"];
    protected $noNeedRight = "*";

    public function lists()
    {
        $this->success("", Lists::with(["subarea", "mode", "level"])->field("desc,createtime", true)->select());
    }

    public function bind_list()
    {
        $lists = Lists::field("desc,createtime", true)->select();
        foreach ($lists as $value) {
            foreach ($value->subarea as $sub) {
                $where = ['game_id' => $value['id'], 'subarea_id' => $sub->id, 'user_id' => $this->auth->id];
                $bind_id = Bindgame::getValue($where, "id");
                if ($bind_id) {
                    $sub->status = 1;
                } else {
                    $sub->status = 0;
                }
                $sub->game_nickname = Bindgame::getValue($where, "game_nickname");
                $sub->game_number = Bindgame::getValue($where, "game_number");
                $sub->bind_id = !empty($bind_id) ? $bind_id : 0;
                $sub->game_avatar = Bindgame::getValue($where, "game_avatar");
            }
        }

        $this->success("", $lists);
    }

    public function bind()
    {
        $id = $this->request->param("bind_id", 0);
        $game_id = $this->request->param("game_id");
        $subarea_id = $this->request->param("subarea_id");
        $game_number = $this->request->param("game_number");
        $game_nickname = $this->request->param("game_nickname");
        $game_avatar = $this->request->param("game_avatar");
        if (!$game_id || !$subarea_id || !$game_number) {
            $this->error("信息不完整");
        }

//

        $exist = Db::name('user_bindgame')->where(['game_number' => $game_number])->find();
        if ($exist) {
            $this->error("该游戏账号已绑定");
        }

        if ($id == 0) {
            $model = new Bindgame();
        } else {
            $model = Bindgame::get($id);
            if ($model->game_id != $game_id || $model->subarea_id != $subarea_id) {
                $this->error("当前绑定信息不对称");
            }

            $record = Record::where(['user_id' => $this->auth->id, "game_id" => $model->game_id, "subarea_id" => $model->subarea_id])->find();
            if ($record) {
                $this->error("您已使用该账号参加过比赛,无法更改");
            }
        }

        Db::startTrans();
        try {
            $model->user_id = $this->auth->id;
            $model->game_id = $game_id;
            $model->subarea_id = $subarea_id;
            $model->game_number = $game_number;
            $model->game_nickname = isset($game_nickname) ? $game_nickname : "";
            $model->game_avatar = isset($game_avatar) ? $game_avatar : "";
            $result = $model->save();
            $task = Userlog::where(['user_id' => $this->auth->id, 'task_id' => 3])->find();
            if (!$task) {
                Userlog::get_task(3, $this->auth->id, 1, 1);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if ($result) {
            $this->success("保存成功");
        }
        $this->error("保存失败");
    }

    public function bind_info()
    {
        $id = $this->request->param("id", 0);
        if ($id == 0) {
            $this->error("当前信息有误");
        }

        $this->success("", Bindgame::field("createtime,updatetime", true)->find($id));
    }


    public function record()
    {
        $lists = Lists::field("id,name,logo")->select();
        foreach ($lists as $value) {
            $value['take_num'] = Db::name('game_record')
                ->where(['user_id' => $this->auth->id, "game_id" => $value['id']])->count();
            $value['crown_num'] = Db::name('game_record')
                ->where(['user_id' => $this->auth->id, "game_id" => $value['id']])
                ->where("rank", 1)
                ->count();
            $value['fail_num'] = Db::name('game_record')
                ->where(['user_id' => $this->auth->id, "game_id" => $value['id']])
                ->where("rank", ">", 4)
                ->count();
        }
        $this->success("", $lists);
    }

    public function record_detail()
    {
        $id = $this->request->param("id", 0);
        $pagesize = $this->request->param("size", 10);

        $lists = Db::name("game_record")->alias("gr")
            ->join("game_lists gl", "gl.id=gr.game_id")
            ->where("gr.game_id", $id)
            ->where("gr.user_id", $this->auth->id)
            ->field("gr.id,gl.name,gl.name,gl.logo,gr.rank as result,FROM_UNIXTIME(gr.createtime,'%Y-%m-%d %H:%i:%s') as create_text")
            ->paginate($pagesize);
//        $this->success("", [
//            [
//                'id' => 1,
//                'name' => Lists::where('id', $id)->value('name'),
//                'logo' => Lists::where('id', $id)->value('logo'),
//                'result' => '结果展示',
//                'create_text' => '2023-08-25 19:25:00'
//            ]
//        ]);
        $this->success("", $lists);
    }

    public function group()
    {
        $game_id = $this->request->param("game_id");
        $subarea_id = $this->request->param("subarea_id");
        $mode_id = $this->request->param("mode_id");
        $level_id = $this->request->param("level_id");

        if (!$game_id || !$subarea_id || !$mode_id || !$level_id) {
            $this->error("参数不全");
        }

        $this->success("", Groups::where(['game_id' => $game_id, 'subarea_id' => $subarea_id, 'mode_id' => $mode_id, 'level_id' => $level_id])->field("third_id", true)->find());
    }
}